package fr.uhp.mpr.tools;

import java.util.ArrayList;

import fr.uhp.mpr.data.DataMeter;
import fr.uhp.mpr.data.Intervalle;


/**
 * Table est l'outil qui permet de construire
 * des tables de probabilites a partir des donnees meteo.
 */
public class Table {
                       
        public Proba[] buildTable1D(Intervalle[] inter, ArrayList<Double> data) {
                double[] tmp = new double[inter.length];

                for (int i = 0; i < data.size(); i++)
                        tmp[DataMeter.findCorrectIntervalle(data.get(i), inter)] ++;
               
                Proba[] table = new Proba[inter.length];
                for (int i = 0; i < table.length; i++)
                        table[i] = new Proba(tmp[i] / data.size());
               
                return table;
        }
       
        public Proba[][] buildTable2D(Intervalle[] inter1, Intervalle[] inter2,
                ArrayList<Double> data1, ArrayList<Double> data2) {
                               
                double[][] tmp = new double[inter1.length][inter2.length];
               
                for (int i = 0; i < data1.size(); i++)
                                tmp[DataMeter.findCorrectIntervalle(data1.get(i), inter1)]
                                   [DataMeter.findCorrectIntervalle(data2.get(i), inter2)] ++;

                double[] sum = new double[inter1.length];
               
                for (int i = 0; i < inter1.length; i++) {
                        for (int j = 0; j < inter2.length; j++)
                                sum[i] += tmp[i][j];
                }
               
                Proba[][] table = new Proba[inter1.length][inter2.length];
               
                for (int i = 0; i < inter1.length; i++) {
                        for (int j = 0; j < inter2.length; j++)
                                table[i][j] =
                                        sum[i] != 0 ?
                                        new Proba((double)tmp[i][j]/sum[i]) :
                                                new Proba((double) 1/inter2.length);
                }
               
                return table;
        }
       
        public Proba[][][] buildTable3D(
                        Intervalle[] inter1, Intervalle[] inter2, Intervalle[] inter3,
                        ArrayList<Double> data1, ArrayList<Double> data2, ArrayList<Double> data3) {
               
                double[][][] tmp = new double[inter1.length][inter2.length][inter3.length];
               
                for (int i = 0; i < data1.size(); i++)
                                tmp[DataMeter.findCorrectIntervalle(data1.get(i), inter1)]
                                   [DataMeter.findCorrectIntervalle(data2.get(i), inter2)]
                                   [DataMeter.findCorrectIntervalle(data3.get(i), inter3)] ++;

                double[] sum = new double[inter1.length * inter2.length];
                int tag = 0, previous = 0;
               
                for (int i = 0; i < inter1.length; i++)
                        for (int j = 0; j < inter2.length; j++){
                                tag = previous == j ? tag : tag+1;
                                previous = j;
                                for (int k = 0; k < inter3.length; k++) {
                                        sum[tag] += tmp[i][j][k];
                                        //System.out.println("sum[" + tag + "]=" + "p[" + i + "][" + j + "][" + k + "]=" + tmp[i][j][k]);
                                }
                        }
                               
                Proba[][][] table = new Proba[inter1.length][inter2.length][inter3.length];
               
                tag = 0; previous = 0;
               
                for (int i = 0; i < inter1.length; i++)
                        for (int j = 0; j < inter2.length; j++){
                                tag = previous == j ? tag : tag+1;
                                previous = j;
                                for (int k = 0; k < inter3.length; k++) {
                                        table[i][j][k] =
                                                sum[tag] != 0 ?
                                                new Proba((double)tmp[i][j][k]/sum[tag]) :
                                                        new Proba((double) 1/inter3.length);
                                        //System.out.println("sum[" + tag + "]=" + "p[" + i + "][" + j + "][" + k + "]=" + tmp[i][j][k]);
                                }
                        }
               
                return table;
        }
       
        /**
         * Parcours l'ensemble des donnees pour remplir une matrice
         * contenant le nombre d'occurrence de chaque configuration.
         */
        public Proba[][][][] buildTable4D(
                        Intervalle[] inter1, Intervalle[] inter2, Intervalle[] inter3, Intervalle[] inter4,
                        ArrayList<Double> data1, ArrayList<Double> data2, ArrayList<Double> data3, ArrayList<Double> data4) {
               
                double[][][][] tmp = new double[inter1.length][inter2.length][inter3.length][inter4.length];
               
                for (int i = 0; i < data1.size(); i++)
                                tmp[DataMeter.findCorrectIntervalle(data1.get(i), inter1)]
                                   [DataMeter.findCorrectIntervalle(data2.get(i), inter2)]
                                   [DataMeter.findCorrectIntervalle(data3.get(i), inter3)]
                                   [DataMeter.findCorrectIntervalle(data4.get(i), inter4)]++;

                double[] sum = new double[inter1.length * inter2.length * inter3.length];
                int tag = 0, previous = 0;
               
                for (int i = 0; i < inter1.length; i++)
                        for (int j = 0; j < inter2.length; j++)
                                for (int k = 0; k < inter3.length; k++) {
                                        tag = previous == k ? tag : tag+1;
                                        previous = k;
                                        for (int l = 0; l < inter4.length; l++)
                                                sum[tag] += tmp[i][j][k][l];
                                }

                Proba[][][][] table = new Proba[inter1.length]
                                               [inter2.length]
                                               [inter3.length]
                                               [inter4.length];
               
                tag = 0; previous = 0;
               
                for (int i = 0; i < inter1.length; i++)
                    for (int j = 0; j < inter2.length; j++)
                        for (int k = 0; k < inter3.length; k++) {
                            tag = previous == k ? tag : tag+1;
                            previous = k;
                            for (int l = 0; l < inter4.length; l++) {
                                table[i][j][k][l] =
                                    		sum[tag] != 0 ?
                                    			new Proba((double)tmp[i][j][k][l]/sum[tag]) :
                                    			new Proba((double) 1/inter4.length);
                            }
                    }
               
                return table;
        }
}